package com.hardydou.framework.filestore.impl;

import cn.hutool.core.io.FileUtil;
import com.hardydou.framework.filestore.FileStoreConfig;
import com.hardydou.framework.filestore.AbstractFileStore;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author ：hardy
 * @description：磁盘文件存储
 * @date ：Created in 2020/6/15
 * @Copyright ：Hardydou.com
 */
public class DiskFileStore extends AbstractFileStore {


    private static String ROOT;

    public DiskFileStore(FileStoreConfig config, FileType fileType, BusType busType, String extName) {
        super(config, fileType, busType, extName);
        ROOT = config.getDiskRoot();
    }

    @Override
    public OutputStream createFile() throws IOException {
        String relPath = calRelativePath();
        String absPath = calFullPath(relPath);
        return FileUtil.getOutputStream(absPath);
    }

    @Override
    public void write(InputStream in) throws IOException {
        FileUtil.writeFromStream(in, calFullPath(calRelativePath()));
    }

    @Override
    public String getReqPath() {
        return getPathPreFix() + calRelativePath();
    }

    @Override
    public OutputStream read(String relativePath) throws IOException {
        return FileUtil.getOutputStream(calFullPath(relativePath));
    }


    private String calFullPath(String relativePath) {
        return ROOT + relativePath;
    }
}
